/* Files include */
#include "dpm32m0xx_it.h"
#include "dpm32m0xx_libopt.h"
//#include "dpm32m030.h"

/**
  * @addtogroup DPM32M030_LibSamples
  * @{
  */

/**
  * @addtogroup Templates
  * @{
  */

/* Private typedef --------------------------------------------------------------*/

/* Private define ---------------------------------------------------------------*/

/* Private macro ----------------------------------------------------------------*/

/* Private variables ------------------------------------------------------------*/

/* Private functions ------------------------------------------------------------*/


/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
		#if LOCKUP_TEST_EN == 1
		*(volatile uint32_t*)0x40032013 = 0x12345678;
		#endif
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}


/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void Param_AssertHandler(uint8_t *file, int16_t line)
{
  /* Users can add file names and line numbers to implement assertion reporting.
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
/******************************************************************************/
/*                 DPM32M0XX Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_dpm32m0xx.s).                                               */
/******************************************************************************/


#include "charger.h"

extern uint8_t gADCconvertPos;
extern uint8_t gADCconvertSeq[];
/**
 * @description: ADC中断服务函数
 * @return {*}
 */
void ADC0_IRQHandler(void){
    if(ADC_GetIntFlagStatus(ADC0, ADC_INT_FLAG_EOS) != RESET){
        ADC_ClearIntFlag(ADC0, ADC_INT_FLAG_EOS);
        /*获取ADC数据*/
		// uint16_t temp1 = 0 , temp2 = 0;
		// temp1 = ADC0->DR_SCAN[0];
		// temp2 = ADC0->DR_SCAN[1];

		// if(temp1 >= charger.battery_L.current_offset){
		// 	charger.battery_L.adc_current.data = temp1 - charger.battery_L.current_offset;	//充电电流偏移
		// }
        // else{
        //     charger.battery_L.adc_current.data = 0;
        // }
		
		// if(temp2 >= charger.battery_R.current_offset){
		// 	charger.battery_R.adc_current.data = temp2 - charger.battery_R.current_offset;	//充电电流偏移
		// }
        // else{
        //     charger.battery_R.adc_current.data = 0;
        // }
		uint16_t temp1 = ADC0->DR_SCAN[2];
		uint16_t temp2 = ADC0->DR_SCAN[3];


        charger.battery_L.adc_current.data = ADC0->DR_SCAN[0] ;//+ 20;
        charger.battery_R.adc_current.data = ADC0->DR_SCAN[1] ;//+ 20;
		if(temp1 > 31){
			charger.battery_L .adc_voltage.data = temp1 - 31;  //31为通道偏移
		}
		if(temp2 > 31){
			charger.battery_R .adc_voltage.data = temp2 - 31;
		}
		
		charger.temp      .            data = ADC0->DR_SCAN[4];
		charger.Vin       .            data = ADC0->DR_SCAN[5];
    }
}


/**
 * @description: 充电器使能关闭 配置为开漏后直接拉低
 * @return {*}
 */
static void Charger_enable_off(void){
	GPIO_InitTypeStruct gpioInitStr;
	gpioInitStr.GPIO_Pin = CHARGE_L_PIN | CHARGE_R_PIN;
    gpioInitStr.GPIO_Mode = GPIO_MODE_OUTPUT;
    gpioInitStr.GPIO_PuPd = GPIO_PUPD_NOPULL;
    gpioInitStr.GPIO_OType = GPIO_OTYPE_OD;
    GPIO_Init(CHARGE_L_PORT , &gpioInitStr);

    DIS_CHARGE_L();
    DIS_CHARGE_R();
}
    


/*
 * @brief  This function handles PVD interrupt request.
 * @param  None
 * @retval None
 */
void PVD_IRQHandler(void){
	if(PVD_GetIntFlagStatus() != RESET)
	{
		PVD_ClearIntFlag();
		Charger_enable_off();
	}
}



/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT DP *****END OF FILE ***************************/
